package com.moon.controller;

import com.moon.domain.User;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;
import java.util.List;

//定义Controller并声明Bean
@Controller
@RequestMapping("/user")
public class UserController {
    //    设置访问路径
    @RequestMapping("/save")
//    设置返回值类型 return内容即response
    @ResponseBody
    public String save() {
        System.out.println("user saving-");
        return "{'module','springmvc'}";
    }

    //    设置访问路径
    @RequestMapping("/delete")
//    设置返回值类型 return内容即response
    @ResponseBody
    public String delete() {
        System.out.println("user saving-");
        return "{'module','springmvc'}";
    }

    //接受普通参数
    @RequestMapping("/commonParam")
    @ResponseBody                           //username赋值name
    public String commonParam(@RequestParam("username") String name, int age) {
        System.out.println("普通参数传递,name = " + name);
        System.out.println("普通参数传递,age = " + age);
        return "{'module','common param'}";
    }

    //接受普通参数
    @RequestMapping("/pojoParam")
    @ResponseBody
    public String pojoParam(User user) {
//  通过get的Params传进name与age自动赋值给对象
        System.out.println("pojo参数传递 = " + user);
        return "{'module','pojo param'}";
    }

    //接受数组参数
    @RequestMapping("/arrayParam")
    @ResponseBody
    public String arrayParam(String[] likes) {
//  通过get的Params传进多个参数自动赋值给数组likes
        System.out.println("数组参数传递 = " + likes);
        return "{'module','array param'}";
    }

    //接受集合参数
    @RequestMapping("/listParam")
    @ResponseBody          //↓必须写 用于参数对应
    public String listParam(@RequestParam() List<String> likes) {
//  通过get的Params传进多个参数自动赋值给数组likes
        System.out.println("集合参数传递 = " + likes);
        return "{'module','list param'}";
    }

    //JSON---------------------------------------------------------------------------------
    //集合参数：json格式
    @RequestMapping("/listParamForJson")
    @ResponseBody
    public String listParamForJson(@RequestBody List<String> likes) {
//
        System.out.println("list common(json)参数传递 list = " + likes);
        return "{'module','list common for json param'}";
    }

    //POJO参数：json格式
    @RequestMapping("/pojoParamForJson")
    @ResponseBody
    public String pojoParamForJson(@RequestBody User user) {
//
        System.out.println("pojo(json)参数传递 user = " + user);
        return "{'module','pojo for json param'}";
    }

    //集合参数：json格式
    @RequestMapping("/listPojoParamForJson")
    @ResponseBody
    public String listPojoParamForJson(@RequestBody List<String> list) {
//
        System.out.println("list pojo(json)参数传递 list = " + list);
        return "{'module','list pojo common for json param'}";
    }

    //----------------------------------------------------------------------------
    @RequestMapping("/dateParam")
    @ResponseBody
    public String dataParam(Date date,
                            @DateTimeFormat(pattern = "yyyy-MM-dd") Date date1,
                            @DateTimeFormat(pattern = "yyyy/MM/dd HH:mm:ss") Date date2) {
        System.out.println("参数传递 date(默认) = " + date);
        System.out.println("参数传递 date1(yyyy-MM-dd) = " + date1);
        System.out.println("参数传递 date2(yyyy/MM/dd HH:mm:ss) = " + date2);
        return "{'module','date param'}";
    }
}
